package com.elinkway.infinitemovies.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.elinkway.infinitemovies.adapter.EpisodePlayAdapter;
import com.elinkway.infinitemovies.async.MoviesHttpAsyncTask;
import com.elinkway.infinitemovies.bean.VideoDataBean;
import com.elinkway.infinitemovies.http.api.MoviesHttpApi;
import com.elinkway.infinitemovies.http.parser.VideoListParser;
import com.elinkway.infinitemovies.ui.activity.VideoDetailActivity;
import com.elinkway.infinitemovies.utils.UIs;
import com.elinkway.infinitemovies.view.SpacesItemDecoration;
import com.le123.ysdq.R;
import com.lvideo.http.bean.LVideoDataHull;

/**
 * Created by zhaopeipei on 2017/4/13.
 */
public class EpisodePageFragment extends BaseFragment {
    public static final String TAG = "EpisodeGridFragment";

    public static final String PAGE_INDEX = "pageIndex";

    public static final String SITE = "site";

    public static final String VIDEO = "video";

    public static final String IS_GRID = "isGrid";

    public static final String SCREENTYPE = "screentype";

    private VideoDetailActivity mVideoDetailActivity;

    private RecyclerView recyclerView;

    private EpisodePlayAdapter mEpisodePlayAdapter;

    private int pageIndex;

    private VideoDataBean mVideoData;

    private String mSite;

    private boolean mIsGrid;

    private boolean mIsVisible;

    private String mScreenType = "halfScreen";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mVideoDetailActivity = (VideoDetailActivity)getActivity();
        Log.i(TAG, "onCreate");
    }

    public static EpisodePageFragment getInstance(int position, VideoDataBean videoDataBean, String site, boolean isGrid, String screentype) {
        EpisodePageFragment fragment = new EpisodePageFragment();
        Bundle args = new Bundle();
        args.putInt(PAGE_INDEX, position);
        args.putString(SITE, site);
        args.putSerializable(VIDEO, videoDataBean);
        args.putBoolean(IS_GRID, isGrid);
        args.putString(SCREENTYPE, screentype);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.i(TAG, "onCreateView");
        View view = inflater.inflate(R.layout.fragment_episode_grid, container, false);
        pageIndex = getArguments().getInt(PAGE_INDEX, 1);
        mVideoData = (VideoDataBean) getArguments().getSerializable(VIDEO);
        mSite = getArguments().getString(SITE, "");
        mIsGrid = getArguments().getBoolean(IS_GRID);
        mScreenType = getArguments().getString(SCREENTYPE);
        recyclerView = (RecyclerView) view.findViewById(R.id.episode_grid_view11);
        recyclerView.addItemDecoration(new SpacesItemDecoration(UIs.dipToPx(5)), -1);
        if (mIsGrid) {
            GridLayoutManager mgr = new GridLayoutManager(getActivity(), 5);
            recyclerView.setLayoutManager(mgr);
            if (mVideoDetailActivity.getEpisodeMapList().get(String.valueOf(pageIndex - 1)) != null
                    && mVideoDetailActivity.getEpisodeMapList().get(String.valueOf(pageIndex - 1)).size() > 0) {
                mEpisodePlayAdapter = new EpisodePlayAdapter(getActivity(), mVideoDetailActivity.getEpisodeMapList().get(String.valueOf(pageIndex - 1)), "grid", mScreenType, pageIndex - 1);
                recyclerView.setAdapter(mEpisodePlayAdapter);
            }
        } else {
            LinearLayoutManager mll = new LinearLayoutManager(getActivity());
            recyclerView.setLayoutManager(mll);
            if (mVideoDetailActivity.getEpisodeMapList().get(String.valueOf(pageIndex - 1)) != null
                    && mVideoDetailActivity.getEpisodeMapList().get(String.valueOf(pageIndex - 1)).size() > 0) {
                mEpisodePlayAdapter = new EpisodePlayAdapter(getActivity(), mVideoDetailActivity.getEpisodeMapList().get(String.valueOf(pageIndex - 1)), "list", mScreenType, pageIndex - 1);
                recyclerView.setAdapter(mEpisodePlayAdapter);
            }
        }
        if (mIsVisible && mEpisodePlayAdapter != null) {
            mVideoDetailActivity.setCurrentShowingEpisodePlayAdapter(mEpisodePlayAdapter);
            mIsVisible = false;
        }
        for (int i = 1; i <= pageIndex; i++) {
            if (mVideoDetailActivity.getEpisodeMapList() != null && (mVideoDetailActivity.getEpisodeMapList().get(String.valueOf(i - 1)) == null
                    || mVideoDetailActivity.getEpisodeMapList().get(String.valueOf(i - 1)).size() <= 0)) {
                new RequestVideoListTask(getActivity(), i).start();
            }
        }
        return view;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        // TODO Auto-generated method stub
        if(!isVisibleToUser){
            mIsVisible = false;
        }else{
            if (mEpisodePlayAdapter != null && mVideoDetailActivity != null) {
                mEpisodePlayAdapter.setmSelectProder(mVideoDetailActivity.currentPlayingPorder());
                mEpisodePlayAdapter.notifyDataSetChanged();
                mVideoDetailActivity.setCurrentShowingEpisodePlayAdapter(mEpisodePlayAdapter);
            }
            mIsVisible = true;
        }
        super.setUserVisibleHint(isVisibleToUser);
    }



    private class RequestVideoListTask extends MoviesHttpAsyncTask<VideoDataBean> {

        int mIndex;

        public RequestVideoListTask(Context context, int index) {
            super(context);
            mIndex = index;
        }

        @Override
        public void netNull() {
            super.netNull();
            UIs.showToast(R.string.video_playsrc_init_failed);// ???
        }

        @Override
        public void netErr(int updateId, String errMsg) {
            super.netErr(updateId, errMsg);
            UIs.showToast(R.string.video_playsrc_init_failed);
        }

        @Override
        public void dataNull(int updateId, String errMsg) {
            super.dataNull(updateId, errMsg);
            UIs.showToast(R.string.video_playsrc_init_failed);
        }

        @Override
        public LVideoDataHull<VideoDataBean> doInBackground() {
            return MoviesHttpApi.requestNewVideoList(new VideoListParser(mSite, mVideoData, mIndex), mSite, mVideoData.getAid(), mIndex);
        }

        @Override
        public void onPostExecute(int updateId, VideoDataBean result) {
            Log.e("zhaopei", "result is " + result);
            mVideoDetailActivity.refreshVideoEpisode(mIsGrid, mIndex - 1, result.getPageEpisodeList().get(String.valueOf(mIndex - 1)));
            if (mIndex == pageIndex) {
                if (mIsGrid) {
                    GridLayoutManager mgr = new GridLayoutManager(getActivity(), 5);
                    recyclerView.setLayoutManager(mgr);
                    mEpisodePlayAdapter = new EpisodePlayAdapter(getActivity(), result.getPageEpisodeList().get(String.valueOf(mIndex - 1)), "grid", mScreenType, mIndex - 1);
                    recyclerView.setAdapter(mEpisodePlayAdapter);
                } else {
                    LinearLayoutManager mll = new LinearLayoutManager(getActivity());
                    recyclerView.setLayoutManager(mll);
                    mEpisodePlayAdapter = new EpisodePlayAdapter(getActivity(), result.getPageEpisodeList().get(String.valueOf(mIndex - 1)), "list", mScreenType, mIndex - 1);
                    recyclerView.setAdapter(mEpisodePlayAdapter);
                }
                if (mIsVisible && mEpisodePlayAdapter != null) {
                    mVideoDetailActivity.setCurrentShowingEpisodePlayAdapter(mEpisodePlayAdapter);
                    mIsVisible = false;
                }
            }

        }
    }

}
